cmake_minimum_required(VERSION 3.10)
project(algorithmAPI)
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}  -fprofile-arcs -g")

if (NOT DEFINED TARGET_ARCH)
    set(TARGET_ARCH pcie)
endif()

if (${TARGET_ARCH} STREQUAL "pcie")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -pthread -fpermissive")

    set(FFMPEG_DIR  /opt/sophon/sophon-ffmpeg-latest/lib/cmake)
    find_package(FFMPEG REQUIRED)
    include_directories(${FFMPEG_INCLUDE_DIRS})
    link_directories(${FFMPEG_LIB_DIRS})

    set(OpenCV_DIR  /opt/sophon/sophon-opencv-latest/lib/cmake/opencv4)
    find_package(OpenCV REQUIRED)
    include_directories(${OpenCV_INCLUDE_DIRS})
    link_directories(${OpenCV_LIB_DIRS})

    set(LIBSOPHON_DIR  /opt/sophon/libsophon-current/data/libsophon-config.cmake)
    find_package(LIBSOPHON REQUIRED)
    include_directories(${LIBSOPHON_INCLUDE_DIRS})
    link_directories(${LIBSOPHON_LIB_DIRS})

    set(BM_LIBS bmlib bmrt bmcv yuv)
    find_library(BMJPU bmjpuapi)
    if(BMJPU)
        set(JPU_LIBS bmjpuapi bmjpulite)
    endif()

    include_directories(../)
    include_directories(../../../framework)
    include_directories(../../../framework/include)

    include_directories(../../../3rdparty/spdlog/include)
    include_directories(../../../3rdparty/nlohmann-json/include)
    include_directories(../../../3rdparty/clipper/include)
    include_directories(../../../3rdparty/httplib)

    include_directories(include/ppocr_det)
    include_directories(include/ppocr_rec/)
    add_library(ppocr_det SHARED
        src/ppocr_det/ppocr_det_pre_process.cc
        src/ppocr_det/ppocr_det_post_process.cc
        src/ppocr_det/ppocr_det_post_processor.cc
        src/ppocr_det/ppocr_det_inference.cc
        src/ppocr_det/ppocr_det.cc
        ../../../3rdparty/clipper/src/clipper.cpp
    )
    add_library(ppocr_rec SHARED
        src/ppocr_rec/ppocr_rec_pre_process.cc
        src/ppocr_rec/ppocr_rec_post_process.cc
        src/ppocr_rec/ppocr_rec_inference.cc
        src/ppocr_rec/ppocr_rec.cc
    )

    target_link_libraries(ppocr_det ${FFMPEG_LIBS} ${OpenCV_LIBS} ${BM_LIBS} ${JPU_LIBS} -lpthread)
    target_link_libraries(ppocr_rec ${FFMPEG_LIBS} ${OpenCV_LIBS} ${BM_LIBS} ${JPU_LIBS} -lpthread)

elseif (${TARGET_ARCH} STREQUAL "soc")
    add_compile_options(-fPIC)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}  -fprofile-arcs -ftest-coverage -g -rdynamic")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}  -fprofile-arcs -ftest-coverage -rdynamic -fpermissive")
    set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
    set(CMAKE_ASM_COMPILER aarch64-linux-gnu-gcc)
    set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)

    include_directories("${SOPHON_SDK_SOC}/include/")
    include_directories("${SOPHON_SDK_SOC}/include/opencv4")
    link_directories("${SOPHON_SDK_SOC}/lib/")
    set(BM_LIBS bmlib bmrt bmcv yuv)
    find_library(BMJPU bmjpuapi)
    if(BMJPU)
        set(JPU_LIBS bmjpuapi bmjpulite)
    endif()

    include_directories(../)
    include_directories(../../../framework)
    include_directories(../../../framework/include)

    include_directories(../../../3rdparty/spdlog/include)
    include_directories(../../../3rdparty/nlohmann-json/include)
    include_directories(../../../3rdparty/clipper/include)
    include_directories(../../../3rdparty/httplib)

    include_directories(include/ppocr_det)
    include_directories(include/ppocr_rec)
    add_library(ppocr_det SHARED
        src/ppocr_det/ppocr_det_pre_process.cc
        src/ppocr_det/ppocr_det_post_process.cc
        src/ppocr_det/ppocr_det_post_processor.cc
        src/ppocr_det/ppocr_det_inference.cc
        src/ppocr_det/ppocr_det.cc
        ../../../3rdparty/clipper/src/clipper.cpp
    )
    add_library(ppocr_rec SHARED
        src/ppocr_rec/ppocr_rec_pre_process.cc
        src/ppocr_rec/ppocr_rec_post_process.cc
        src/ppocr_rec/ppocr_rec_inference.cc
        src/ppocr_rec/ppocr_rec.cc
    )

    target_link_libraries(ppocr_det ${FFMPEG_LIBS} ${OpenCV_LIBS} ${BM_LIBS} ${JPU_LIBS} -fprofile-arcs -lgcov -lpthread)
    target_link_libraries(ppocr_rec ${FFMPEG_LIBS} ${OpenCV_LIBS} ${BM_LIBS} ${JPU_LIBS} -fprofile-arcs -lgcov -lpthread)
endif()
